package com.lagou;

import com.lagou.bean.ConsumerComponent;
import com.lagou.filter.DubboInvokeFilter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;


public class AnnotationConsumerMain{


    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);

        List<Map<String, String>> methodsTimeList;
        ExecutorService executor = Executors.newCachedThreadPool();
        executor.execute(() ->{
            for (int i = 0; i < 2000; i++) {
                service.methodA();
                service.methodB();
                service.methodC();
            }
        });
        /*executor.shutdown();
        while (true) {
            if (executor.isTerminated()) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }*/
        methodsTimeList = DubboInvokeFilter.getTimeContainer();
        while (true) {
            tp(methodsTimeList, "methodA");
            tp(methodsTimeList, "methodB");
            tp(methodsTimeList, "methodC");
            Thread.sleep(5000);
        }


    }

    /**
     * 计算TP90、TP99的值
     * @param list
     * @param methodName
     */
    private static void tp(List<Map<String, String>> list, String methodName){
        List<Integer> timeList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Map<String, String> map = list.get(i);
            String value = map.get(methodName);
            if (!StringUtils.isEmpty(value)) {
                if (Long.parseLong(value.split("-")[0])>=System.currentTimeMillis() - 60000) {
                    timeList.add(Integer.parseInt(value.split("-")[1]));
                }
            }
        }


        if (timeList != null && timeList.size() > 0) {
            Collections.sort(timeList);
            int tp90 = timeList.get((int) (timeList.size() * 0.9));
            int tp99 = timeList.get((int) (timeList.size() * 0.99));

            System.out.println(methodName+" tp90:"+tp90);
            System.out.println(methodName+" tp99:"+tp99);
            System.out.println("-------------------------");
        }

    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    //@EnableDubbo(scanBasePackages = "com.lagou.bean")
    @ComponentScan("com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
